/**
 * @file XListViewHeader.java
 * @create Apr 18, 2012 5:22:27 PM
 * @author Maxwin
 * @description XListView's header
 */
package com.naicheng.ivote.uiself;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XListViewHeader extends LinearLayout {
	private LinearLayout mContainer;
	private TextView mHintTextView;
	private int mState = STATE_NORMAL;

	public int num = 0;
//	public String num = 1;
	
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_REFRESHING = 2;

	public XListViewHeader(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public XListViewHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		// 初始情况，设置下拉刷新view高度为0
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 0);
		mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.xlistview_header, null);
		addView(mContainer, lp);
		setGravity(Gravity.BOTTOM);

		mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview);
		
	}

	public void setState(int state) {
		if (state == mState) return ;
		
		switch(state){
		case STATE_NORMAL:
			
			mHintTextView.setText(R.string.xlistview_header_hint_normal);
			break;
		case STATE_READY:
			if (mState != STATE_READY) {
				mHintTextView.setText(R.string.xlistview_header_hint_normal);
			}
			break;
		case STATE_REFRESHING:
			num=0;
			jindu();
			break;
			default:
		}
		
		mState = state;
	}
	
	/**
	 * 刷新中文字动画
	 */
	public void jindu(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true==ConstantMine.ISreflesh) {
					if(num>3){
						num=0;
					}
					num++;
					System.out.println("num:"+num);
					mhan.sendEmptyMessage(num);
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					
					
				}
				
				
			}
		}).start();
	}
	
	private Handler mhan=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				mHintTextView.setText(R.string.xlistview_header_hint_loading1);
				break;
			case 2:
				mHintTextView.setText(R.string.xlistview_header_hint_loading2);
				break;
			case 3:
				mHintTextView.setText(R.string.xlistview_header_hint_loading3);
				break;

			}
		}
		
	};
	public void setVisiableHeight(int height) {
		if (height < 0)
			height = 0;
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
				.getLayoutParams();
		lp.height = height;
		mContainer.setLayoutParams(lp);
	}

	public int getVisiableHeight() {
		return mContainer.getHeight();
	}

}
